﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.Text.RegularExpressions;

namespace YouHP.Pages.User
{
    public class BasicInfoAdd : UserBasePage
    {
        protected UserInfo _User = new UserInfo();
        public List<DistrictInfo> mProvinceList = new List<DistrictInfo>();
        protected DistrictInfo district = new DistrictInfo();
        protected DistrictInfo district1 = new DistrictInfo();

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;



            if (RequestTools.IsPost()) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            this.AddLocation("个人信息和联系方式", "/User/BasicInfoAdd.aspx");
            this.SetTitle("个人信息和联系方式");
            mProvinceList = DistrictBLL.Instance.GetDistrictListByParent(0);
            this.AddDatePicker();

            _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            UserData = _User;
            district = DistrictBLL.Instance.GetDistrict(_User.ProvinceId);
            district1 = DistrictBLL.Instance.GetDistrict(_User.CityId);
        }

        protected void HandleGet()
        {
            /*将用户更改完的生日填充到select列表中、、、to be done.
            DateTime userBirthday = _User.Birthday;
            string yearValue = userBirthday.Date.Year.ToString();
            string monthValue = userBirthday.Date.Month.ToString();
            string dayValue = userBirthday.Date.Day.ToString();
            Response.Write("<script>var year = document.getElementById('year');year.options[year.selectedIndex].value = "+yearValue+"</script>");(有问题，哪位大仙帮我看看啊)
            */
        }

        protected void HandlePost()
        {

            if (RequestTools.GetString("description").Length > 500)
            {
                //如果自我介绍超过500...
                AddErrLine("自我介绍不得超过500个字符");
                return;
            }
            if (RequestTools.GetString("interest").Length > 50)
            {
                //如果兴趣超过50...
                AddErrLine("兴趣介绍不得超过50个字符");
                return;
            }

            UserInfo _User = UserBLL.Instance.GetUserInfo(UserData.Id);

            _User.Gender = RequestTools.GetString("sex");

            string birthday = "";
            try
            {
                string year = RequestTools.GetString("year");
                string month = RequestTools.GetString("month");
                string day = RequestTools.GetString("day");
                birthday = year + "-" + month + '-' + day;
                _User.Birthday = Convert.ToDateTime(birthday);
            }
            catch
            {
                birthday = "1800-01-01";
                _User.Birthday = Convert.ToDateTime(birthday);
            }

            _User.Interests = Utils.HtmlEncode(RequestTools.GetString("interest"));

            _User.Description = Utils.HtmlEncode(RequestTools.GetString("description"));



            if (RequestTools.GetString("mobile").Trim() != "" && !Regex.IsMatch(RequestTools.GetString("mobile").Trim(), @"^[\d|-]+$"))
            {
                AddErrLine("移动电话号码中含有非法字符");
            }

            if (RequestTools.GetString("phone").Trim() != "" &&
                !Regex.IsMatch(RequestTools.GetString("phone").Trim(), @"^[\d|-]+$"))
            {
                AddErrLine("固定电话号码中含有非法字符");
            }

            _User.MobilePhone = RequestTools.GetString("mobile");
            _User.Telephone = RequestTools.GetString("phone");
            _User.ZipCode = RequestTools.GetString("zip");

            _User.MSN = RequestTools.GetString("msn");
            _User.QQ = RequestTools.GetString("qq");
            _User.ProvinceId = RequestTools.GetFormInt("province", 0);
            _User.CityId = RequestTools.GetFormInt("city", 0);
            _User.Address = RequestTools.GetFormString("address");//病人详细地址

            UserBLL.Instance.UpdateUserProfile(_User);

            AddMsgLine("个人信息和联系方式添加完毕");
            SetUrl("/Profile/Index.aspx?uid="+UserData.Id);
            SetMetaRefresh();

        }
    }
}
